package cn.middle.remote.message;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import cn.middle.remote.util.ConversionUtil;

public class ReplyMessageSignal extends BaseSignal {
	
	private byte[] data;
	
	private String interfaceName;
	
	private String message;
	
	private int remoteAddressClientId;
	
	/**
	 * 一个标识，表示调用是否正常。 1正常，其它异常。网络情况有时候可能会有波动，比如园区的目标的客户端没有登陆到服务器，或者其它的异常导致调用失败。
	 */
	private int code;

	public int getRemoteAddressClientId() {
		return remoteAddressClientId;
	}

	public void setRemoteAddressClientId(int remoteAddressClientId) {
		this.remoteAddressClientId = remoteAddressClientId;
	}

	public byte[] getData() {
		return data;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public ReplyMessageSignal() {
		super(SignalHelper.REPLY);
	}

	public String getInterfaceName() {
		return interfaceName;
	}

	public void setInterfaceName(String interfaceName) {
		this.interfaceName = interfaceName;
	}

	public void setData(byte[] data) {
		this.data = data;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	@Override
	public byte[] toData() {
		ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
		DataOutputStream out = new DataOutputStream(bos);
		try {
			out.writeInt(remoteAddressClientId);
			out.writeInt(code);
			byte[] bytes  = ConversionUtil.writeStringBytes(message);
			if(bytes.length == 0) {
				out.writeInt(0);
			}else {
				out.write(bytes);
			}
			out.write(ConversionUtil.writeStringBytes(interfaceName));
			out.write(ConversionUtil.writeObjectBytes(data));
			out.flush();
			return bos.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new byte[0];
	}

}
